class Solution
{
public:
    int countTriplets(vector<int> &arr)
    {
        int n = arr.size();
        int xorPrefix = 0;
        unordered_map<int, int> prefixFreq;
        unordered_map<int, int> prefixIndexSum;
        prefixFreq[0] = 1;
        prefixIndexSum[0] = -1;
        int result = 0;
        for (int i = 0; i < n; ++i)
        {
            xorPrefix ^= arr[i];
            if (prefixFreq.count(xorPrefix))
            {
                result += prefixFreq[xorPrefix] * (i - 1) - prefixIndexSum[xorPrefix];
            }
            ++prefixFreq[xorPrefix];
            prefixIndexSum[xorPrefix] += i;
        }
        return result;
    }
};